﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.CallCenter.Modules
{
    public partial class RestaurantList : System.Web.UI.UserControl
    {
        protected List<int> catList = new List<int>();
        protected List<RestaurantModel> pageDataList
        {
            get;
            set;
        }

        public int AreaId
        {
            get;
            set;
        }

        public string RestaurantName
        {
            get;
            set;
        }

        public string DishesName
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var pageCount = 0;
            var restaurantIds = ",";
            pageDataList = RestaurantManager.Search(GetSearchCondition(), 0, 0, out pageCount);
            //pageDataList = dataList.OrderBy(item => item.Category).ToList<RestaurantModel>();
            pageDataList.ForEach(
                    item =>
                    {
                        if (!catList.Contains(item.Category))
                        {
                            catList.Add(item.Category);
                        }
                        if (restaurantIds.IndexOf("," + item.Id.ToString() + ",") < 0)
                        {
                            restaurantIds += item.Id.ToString() + ",";
                        }
                    }
                );
            if (!string.IsNullOrEmpty(DishesName) && pageDataList.Count > 0)
            {
                var sqlWhere = " Name like'" + DishesName.Trim() + "%'";
                sqlWhere += " and RestaurantId in(" + restaurantIds.Trim(',') + ")";
                var dishesList = RestaurantManager.SearchDishes(sqlWhere, 0, 0, out pageCount);
                var restIds = new List<long>();
                dishesList.ForEach(item =>
                {
                    if (restIds.Contains(item.RestaurantId) == false)
                    {
                        restIds.Add(item.RestaurantId);
                    }
                });

                var newPageDataList = from item in pageDataList
                                      where restIds.Contains(item.Id) == true
                                      select item;

                pageDataList = newPageDataList.ToList<RestaurantModel>();
                catList.Clear();
                pageDataList.ForEach(item => {
                    if (!catList.Contains(item.Category))
                    {
                        catList.Add(item.Category);
                    }
                });
            }
        }

        private string GetSearchCondition()
        {
            var sqlWhere = " AreaId=" + AreaId;

            if (string.IsNullOrEmpty(RestaurantName) == false)
            {
                sqlWhere += " and name like'%" + RestaurantName.Trim() + "%'";
            }
            return sqlWhere;
        }
    }
}